import FIELD_VALUE_TYPE from "../constants/commonConstants"
import TextFieldValue from "../field/showFieldValue/TextFieldValue"
import LabelFieldValue from "../field/showFieldValue/LabelFieldValue"
import Field from "../field/Field"
import SwitchFieldValue from "../field/showFieldValue/SwitchFieldValue"
import TextListFieldValue from "../field/showFieldValue/TextListFieldValue"
import FieldGroup from "../field/FieldGroup"
import classLoader from "./classLoader"
import FieldHandler from "./FieldHandler"
import JSONFieldHandler from "./JSONFieldHandler"


const LOADER_FIELD_LIB = {
	fieldLib:{}
};
function loadField(code,path,field,creator){
	path = path|| "";
	let pfield = {
		path:path,
		field:field,
		creator:creator
	};
	
	if(LOADER_FIELD_LIB.fieldLib[code]){
		LOADER_FIELD_LIB.fieldLib[code].push(pfield);
	}else{
		LOADER_FIELD_LIB.fieldLib[code] = [].concat(pfield);
	}
	classLoader.loadClass(code,field,creator,path)
}
function getFieldPo(code,path){
	let rtList =  LOADER_FIELD_LIB.fieldLib[code];
	if(rtList && rtList.length > 0){
		if(!path){
			return rtList[0];
		}
		for(let pfield of rtList){
			if(pfield.path == path){
				return pfield;
			}
		}
	}
	return null;
}
function getField(code,path){
	let f = getFieldPo(code,path);
	if(f!= null){
		return f.field;
	}
	return null;
}

loadField(FIELD_VALUE_TYPE.show.TEXT.code,FIELD_VALUE_TYPE.show.TEXT.clsPage,TextFieldValue,function(params){
	return new TextFieldValue(...arguments);
});
loadField(FIELD_VALUE_TYPE.show.LABEL.code,FIELD_VALUE_TYPE.show.LABEL.clsPage,LabelFieldValue,function(params){
	return new LabelFieldValue(...arguments);
});
loadField(FIELD_VALUE_TYPE.show.TEXT_LIST.code,FIELD_VALUE_TYPE.show.TEXT_LIST.clsPage,TextListFieldValue,function(params){
	return new TextListFieldValue(...arguments);
});
loadField(FIELD_VALUE_TYPE.show.SWITCH.code,FIELD_VALUE_TYPE.show.SWITCH.clsPage,SwitchFieldValue,function(params){
	return new SwitchFieldValue(...arguments);
});

loadField(FIELD_VALUE_TYPE.base.FieldGroup.code,FIELD_VALUE_TYPE.base.FieldGroup.clsPage,FieldGroup,function(params){
	return new FieldGroup(...arguments);
});

loadField(FIELD_VALUE_TYPE.base.Field.code,FIELD_VALUE_TYPE.base.Field.clsPage,Field,function(params){
	return new Field(...arguments);
});
// 特殊处理  fieldHandler 与  JSONFieldHandler
loadField("FieldHandler","fieldHandler",FieldHandler,function(params){
	return new FieldHandler(...arguments);
});
loadField("JSONFieldHandler","fieldHandler",JSONFieldHandler,function(params){
	return new JSONFieldHandler(...arguments);
});


function createFieldGroup(name,code){
	return new FieldGroup(name,code);
}
function createFieldValue(code){
	let f = getFieldPo(code,null);
	let params = [];
	
	for(let aix = 1 ; aix < arguments.length; aix ++){
		params.push(arguments[aix]);
	}
	if(f!= null && f.creator != null ){
		return f.creator(...params);
	}
	return null;
}
function createField(fieldValueType,name,code,defaultFieldValue,value){
	let fv = null;
	if(!fieldValueType){
		fv = defaultFieldValue;
	}else{
		fv = createFieldValue(fieldValueType);
		if(fv != null){
			fv.defaultValue = defaultFieldValue;
			fv.value = value;
		}
			
	}
	return new Field(name,code,fv);
}

const fieldLoader ={
	loadField,
	getField,
	createFieldGroup,
	createField
	
};

export default fieldLoader